package com.summer.utils.email;

import com.google.common.collect.Lists;
import com.summer.utils.json_serialization.JsonMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
import java.util.Properties;

public class MailTest {
    protected static final Logger logger = LoggerFactory.getLogger(MailTest.class);
    JsonMapper mapper = JsonMapper.nonEmptyMapper();
    public static final String username="customer_service";
    public static final String from = "customer_service@jd.com";
    public static final String password = "360buy.com123!!@@";

    public static final String host = "smtp.jd.com";
    public static final String protocol = "smtp";
    public static void main(String[] args) {
        final Properties props = new Properties();
            props.put("mail.smtp.auth", "true");//需要身份验证
            props.put("mail.smtp.host", host);//服务器主机名
            props.put("mail.user", from); //发件人地址
            props.put("mail.password", password);//密码
            // 发送邮件协议名称
//        props.setProperty("mail.transport.protocol", protocol);

            // 构建授权信息，用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    return new PasswordAuthentication(username, password);
                }
            };
            Session mailSession = Session.getInstance(props, authenticator);
            MimeMessage message = new MimeMessage(mailSession);
            try {
                // 设置发件人
                InternetAddress form = new InternetAddress(
                        props.getProperty("mail.user"));
                message.setFrom(form);
                List<String> receivers = Lists.newArrayList();
                receivers.add("tanwei8@jd.com");
                if (receivers == null || receivers.size() == 0) {
                    logger.info("--send e-mail appear exc,没有任何收件人信息，报警信息无法发送……");
//                    return JsonCommonResponse.failRes("发送邮件，收件人为空");
                }
                for (String receiver : receivers) {
                    // 收件人
                    message.addRecipients(Message.RecipientType.TO, receiver);
                }
                message.setSubject("测试附件");
//                message.setContent("测试主题", "text/html;charset=UTF-8");
                message.setFileName("c://test.txt");

                // 发送邮件
                Transport.send(message);
                File file = new File("c://test.xls");
//                file.deleteOnExit();
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("--send e-mail appear exc，", e);
//                return JsonCommonResponse.failRes("发送邮件，出现错误："+e.getMessage());
            }
//            return JsonCommonResponse.succJessRes();
        }
}
